package com.yiboshi.rehrs.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.yiboshi.rehrs.config.RehrsSystemConfig;
import com.yiboshi.rehrs.domain.entity.SystemConfig;
import com.yiboshi.rehrs.domain.mapper.SystemConfigMapper;
import com.yiboshi.rehrs.domain.service.ISystemConfigService;
import com.yiboshi.rehrs.param.smurfs.Product;
import com.yiboshi.rehrs.util.CacheUtils;
import com.yiboshi.rehrs.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author local
 * @since 2025-07-07
 */
@Service
public class SystemConfigServiceImpl extends AperBaseServiceImpl<SystemConfigMapper, SystemConfig> implements ISystemConfigService {



    @Autowired
    StringRedisTemplate stringRedisTemplate;



    @Override
    public SystemConfig getOne(String key) {
        String keyNew = "cache:systemConfig:"+key;
        SystemConfig systemConfig = CacheUtils.getByCache10Min(keyNew, SystemConfig.class);
        if(systemConfig!=null){
            return systemConfig;
        }
        String cacheConfig = stringRedisTemplate.opsForValue().get(keyNew);
        if(cacheConfig!=null){
            systemConfig = JSON.parseObject(cacheConfig,SystemConfig.class);
            CacheUtils.putByCache10Min(keyNew,systemConfig);
            return systemConfig;
        }
        if(systemConfig==null){
            systemConfig = getOne(getQueryWrapper().eq(SystemConfig::getConfigKey, key)
                    .eq(SystemConfig::getDeleted, 0)
                    .last("order by id desc limit 1")
            );
            if(systemConfig!=null){
                CacheUtils.putByCache10Min(keyNew,systemConfig);
                stringRedisTemplate.opsForValue().set(keyNew,JSON.toJSONString(systemConfig),
                        20, TimeUnit.HOURS);
            }
        }
        return systemConfig;
    }

    @Override
    public List<SystemConfig> list(String key) {
        LambdaQueryWrapper<SystemConfig> systemConfigLambdaQueryWrapper =
                getQueryWrapper().likeRight(SystemConfig::getConfigKey, key)
                .eq(SystemConfig::getDeleted, 0)
                .orderByAsc(SystemConfig::getSortNum);
        return  list(systemConfigLambdaQueryWrapper);
    }

    public List<Product> productList(boolean cache){
        Cache<String, Object> objectCache = CacheUtils.builder(3600);
        String cacheKey = "productList";
        List<Product> products =cache? (List<Product>) objectCache.getIfPresent(cacheKey):null;
        if(products==null){
            List<SystemConfig> orderProduct = list("ldxp_product_");
            products = new ArrayList<>();
            for (SystemConfig systemConfig : orderProduct) {
                Product product = new Product();
                product.setId(systemConfig.getConfigValue());
                String keyMark = systemConfig.getKeyMark();
                if(keyMark.contains("1天体验")&& !RehrsSystemConfig.isDev()){
                    continue;
                }
                product.setName(systemConfig.getKeyMark());
                String[] otherValue = systemConfig.getOtherValue().split(":");
                product.setVipDays(Integer.parseInt(otherValue[0]));
                product.setPrice(otherValue[1]);
                products.add(product);
            }
            objectCache.put(cacheKey,products);
        }
        return products;
    }

    @Override
    public int exchangeVipDays() {
        SystemConfig inviteDays = this.getOne("invite_days");
        if(inviteDays==null){
            return 0;
        }
        String configValue = inviteDays.getConfigValue();
        if(StrUtils.isBlank(configValue)){
            return 0;
        }
        return Integer.valueOf(configValue);
    }
}
